﻿/***********************************************************************
 * <copyright file="BusinessRule.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Thursday, October 17, 2013
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

namespace BusinessObjects.BusinessRules
{
    /// <summary>
    ///     Abstract base class for business rules.
    ///     Maintains property name to which rule applies and validation error message.
    /// </summary>
    public abstract class BusinessRule
    {
        /// <summary>
        ///     Constructor
        /// </summary>
        /// <param name="propertyName">The property name to which rule applies.</param>
        protected BusinessRule(string propertyName)
        {
            PropertyName = propertyName;
            ErrorMessage = propertyName + " is not valid";
        }

        /// <summary>
        ///     Overloaded constructor
        /// </summary>
        /// <param name="propertyName">The property name to which rule applies.</param>
        /// <param name="errorMessage">The error message.</param>
        protected BusinessRule(string propertyName, string errorMessage)
            : this(propertyName)
        {
            ErrorMessage = errorMessage;
        }

        /// <summary>
        ///     Gets or sets the name of the property.
        /// </summary>
        /// <value>
        ///     The name of the property.
        /// </value>
        public string PropertyName { get; set; }

        /// <summary>
        ///     Gets or sets the error message.
        /// </summary>
        /// <value>
        ///     The error message.
        /// </value>
        public string ErrorMessage { get; set; }


        /// <summary>
        ///     Validation method. To be implemented in derived classes.
        /// </summary>
        /// <param name="businessObject">The business object.</param>
        /// <returns>Boolean value</returns>
        public abstract bool Validate(BusinessObject businessObject);


        /// <summary>
        ///     Gets value for given business object's property using reflection.
        /// </summary>
        /// <param name="businessObject">The business object.</param>
        /// <returns>Object value</returns>
        protected object GetPropertyValue(BusinessObject businessObject)
        {
            return businessObject.GetType().GetProperty(PropertyName).GetValue(businessObject, null);
        }
    }
}